<template>
  <el-dialog :close-on-click-modal="false" :title="isEdit ? '修改' : '新增'" :visible.sync="visible">
    <el-form ref="form" :model="form" label-width="150px" :rules="rules">
      <el-form-item label="父级分类">
        <!-- <el-select v-model="form.parentId" style="width: 100%">
          <el-option
            v-for="(item,index) in parentArr"
            :key="index"
            :value="item.evalTypeId"
            :label="item.categoryName"
          />
        </el-select>-->
        <classify-select
          ref="classifySelect"
          :selectValue="form.parentId"
          @select="value => handleTreeSelect('parentId', value)"
        />
      </el-form-item>
      <el-form-item label="资产分类名称" prop="categoryName">
        <el-input v-model="form.categoryName" autocomplete="off" placeholder="请输入资产分类名称" />
      </el-form-item>
      <el-form-item label="折旧年限" prop="serviceLife">
        <el-input v-model.number="form.serviceLife" placeholder="请输入折旧年限" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button :loading="loading" type="primary" @click="handleOk">{{ isEdit ? '修 改' : '新 增' }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { openNotification } from '@/utils/util'
import { categoryCreate, categoryUpdate } from '@/api/assetsNew'
import ClassifySelect from '../components/ClassifySelect'

export default {
  components: {
    ClassifySelect
  },
  data() {
    return {
      loading: false,
      form: {
        parentId: null,
        categoryName: null
      },
      parentArr: [],
      rules: {
        categoryName: [{ required: true, message: '请输入资产分类名称', trigger: 'blur' }],
        serviceLife: [
          { required: true, message: '请输入折旧年限', trigger: 'blur' },
          { type: 'number', message: '请输入数字', trigger: 'blur' },
          { type: 'number', min: 0, message: '输入数字需大于0', trigger: 'blur' }
        ]
      },
      visible: false,
      mdl: {},
      isEdit: false
    }
  },
  methods: {
    handleTreeSelect(key, value) {
      this.$set(this.form, key, value.id)
    },
    edit(bool, row) {
      console.log('bool', bool, row)
      this.isEdit = bool
      this.visible = true
      this.form = Object.assign({}, row)
      if (!bool) {
        this.$nextTick(() => {
          this.$set(this.form, 'parentId', null)
          this.$refs.classifySelect.reset()
        })
      }

      console.log('form', this.form)
    },
    handleOk() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          this.loading = true
          let res = ''
          let mes = ''
          if (this.isEdit) {
            // 修改
            res = await categoryUpdate(this.form)
            mes = '修改操作成功'
          } else {
            // 新增
            res = await categoryCreate(this.form)
            mes = '新增操作成功'
          }
          openNotification('success', mes)
          this.loading = false
          this.visible = false
          this.$emit('refresh')
        }
      })
    }
  }
}
</script>